package form;

import connection.HostManager;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JTextField;

/**
 *
 * @author christ66
 */
public class HostDialog extends javax.swing.JDialog {

    public HostDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        cancelButton2.setVisible(false);
        imageLabel.setVisible(false);
        mainFrame = (MainFrame) parent;
        setTitle("Please Enter a Port");

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextPane1 = new javax.swing.JTextPane();
        jLabel1 = new javax.swing.JLabel();
        label1 = new java.awt.Label();
        portText = new javax.swing.JTextField();
        connectionStatus = new javax.swing.JLabel();
        listenButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        cancelButton2 = new javax.swing.JButton();
        nickName = new javax.swing.JTextField();
        nickNameLabel = new javax.swing.JLabel();
        imageLabel = new javax.swing.JLabel();

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTextPane1.setText("Please Enter a Port");
        jTextPane1.setName("jTextPane1"); // NOI18N
        jScrollPane1.setViewportView(jTextPane1);

        jLabel1.setText("jLabel1");
        jLabel1.setName("jLabel1"); // NOI18N

        label1.setName("label1"); // NOI18N
        label1.setText("label1");

        setAlwaysOnTop(true);
        setIconImages(getIconImages());
        setMinimumSize(new java.awt.Dimension(300, 200));
        setModalExclusionType(null);
        setModalityType(null);
        setName("HostDialog"); // NOI18N
        setResizable(false);
        setSize(new java.awt.Dimension(800, 900));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });
        getContentPane().setLayout(null);

        portText.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        portText.setText("5678");
        portText.setName("portText"); // NOI18N
        portText.setPreferredSize(new java.awt.Dimension(50, 22));
        portText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                portTextActionPerformed(evt);
            }
        });
        getContentPane().add(portText);
        portText.setBounds(140, 20, 50, 22);

        connectionStatus.setText("Enter a Valid Port");
        connectionStatus.setFocusable(false);
        connectionStatus.setInheritsPopupMenu(false);
        connectionStatus.setMaximumSize(new java.awt.Dimension(125, 22));
        connectionStatus.setMinimumSize(new java.awt.Dimension(125, 22));
        connectionStatus.setName("connectionStatus"); // NOI18N
        connectionStatus.setPreferredSize(new java.awt.Dimension(125, 22));
        connectionStatus.setRequestFocusEnabled(false);
        connectionStatus.setVerifyInputWhenFocusTarget(false);
        getContentPane().add(connectionStatus);
        connectionStatus.setBounds(110, 0, 125, 22);

        listenButton.setMnemonic('L');
        listenButton.setText("Listen");
        listenButton.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        listenButton.setName("listenButton"); // NOI18N
        listenButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                listenButtonActionPerformed(evt);
            }
        });
        getContentPane().add(listenButton);
        listenButton.setBounds(60, 50, 70, 20);

        cancelButton.setText("Cancel");
        cancelButton.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        cancelButton.setName("cancelButton"); // NOI18N
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });
        getContentPane().add(cancelButton);
        cancelButton.setBounds(200, 50, 70, 20);

        cancelButton2.setMnemonic('S');
        cancelButton2.setText("Stop");
        cancelButton2.setToolTipText("Stop listening");
        cancelButton2.setMaximumSize(new java.awt.Dimension(70, 22));
        cancelButton2.setMinimumSize(new java.awt.Dimension(70, 22));
        cancelButton2.setName("cancelButton2"); // NOI18N
        cancelButton2.setPreferredSize(new java.awt.Dimension(70, 22));
        cancelButton2.setVerifyInputWhenFocusTarget(false);
        cancelButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(cancelButton2);
        cancelButton2.setBounds(90, 50, 70, 22);

        nickName.setMinimumSize(new java.awt.Dimension(14, 25));
        nickName.setName("nickName"); // NOI18N
        nickName.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                keyTypedActionPerformed(evt);
            }
        });
        getContentPane().add(nickName);
        nickName.setBounds(0, 100, 280, 30);

        nickNameLabel.setText("Nickname");
        nickNameLabel.setName("nickNameLabel"); // NOI18N
        getContentPane().add(nickNameLabel);
        nickNameLabel.setBounds(10, 80, 70, 16);

        imageLabel.setBackground(java.awt.Color.gray);
        imageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/busy.gif"))); // NOI18N
        imageLabel.setDoubleBuffered(true);
        imageLabel.setIgnoreRepaint(true);
        imageLabel.setName("imageLabel"); // NOI18N
        imageLabel.setOpaque(true);
        getContentPane().add(imageLabel);
        imageLabel.setBounds(200, 50, 30, 30);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void listenButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_listenButtonActionPerformed
        hManager = mainFrame.getHostMan();
        cancelButton2.setVisible(true);
        imageLabel.setVisible(true);
        cancelButton.setVisible(false);
        listenButton.setVisible(false);
        portText.setVisible(false);
        nickName.setEditable(false);
        connectionStatus.setText("...Listening...");
        hManager.setDialog(this);
        hManager.start();
        
    }//GEN-LAST:event_listenButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        mainFrame.resetHostMenuItems();
        dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void portTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_portTextActionPerformed
        //validate port
    }//GEN-LAST:event_portTextActionPerformed

    private void cancelButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButton2ActionPerformed
        try {
            hManager.stopHost();
            cancelButton2.setVisible(false);
            imageLabel.setVisible(false);
            cancelButton.setVisible(true);
            listenButton.setVisible(true);
            portText.setVisible(true);
            nickName.setEditable(true);
            connectionStatus.setText("        ");
            mainFrame.resetHostMenuItems();
            dispose();
        } catch (IOException ex) {
            //Logger.getLogger(HostDialog.class.getName()).log(Level.SEVERE, null, ex);
        	dispose();
        }
    }//GEN-LAST:event_cancelButton2ActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        try {
            cancelButtonActionPerformed(null);
            cancelButton2ActionPerformed(null);
        }
        catch (NullPointerException e) {
            // It will reach here only if I am NOT polling for a connection.
            // We do not not care if it fails to close a connection.
        }
        finally {
            dispose();
        }
    }//GEN-LAST:event_formWindowClosing

    private void keyTypedActionPerformed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_keyTypedActionPerformed
        if (((JTextField) evt.getSource()).getText().length() > 24) {
            evt.setKeyChar('\n');
        } else {
            MainFrame.currentPlayer.setNickName(nickName.getText() + evt.getKeyChar());
        }
    }//GEN-LAST:event_keyTypedActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                HostDialog dialog = new HostDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton cancelButton2;
    private javax.swing.JLabel connectionStatus;
    private javax.swing.JLabel imageLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextPane jTextPane1;
    private java.awt.Label label1;
    private javax.swing.JButton listenButton;
    private javax.swing.JTextField nickName;
    private javax.swing.JLabel nickNameLabel;
    private javax.swing.JTextField portText;
    // End of variables declaration//GEN-END:variables
    private HostManager hManager;
    private ImageIcon image;
    private MainFrame mainFrame;
}
